'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('memor-pool')
	const news = db.collection('news') // 消息库
	if (event.type && event.type == 'zan') {
		let list =  await collection.doc(event._id).get();
		// console.log(list)
		// 再此处理添加消息库
		let newslist =  await news.where({memor_id:event._id}).get();
		// console.log(newslist);
		// return
		event.zan.zan_status = event.zan_status;  // 把点赞状态存进对象，方便判断点赞类型
		if(newslist.data.length > 0){  // 证明有此条内容的消息通知
			if(newslist.data[0].news_zan){
				newslist.data[0].news_zan.unshift(event.zan); // 保存到数据头部
			}else{
				let news_zan = [];
				news_zan.unshift(event.zan);
				newslist.data[0].news_zan = news_zan;
			}
			
			await news.where({memor_id:event._id}).update({  // 更新数据库
				is_read: 2,  // 1:已读 2:未读
				news_zan: newslist.data[0].news_zan
			})
		}else{ // 没有此条内容的消息通知
			await news.add({  // 更新数据库
				memor_id: event._id,
				memor_uid: event.memor_uid,
				memor_title: list.data[0].title,
				is_read: 2,  // 1:已读 2:未读
				news_zan: [event.zan]
			})
		}
		// 添加消息库end
		
		if(event.zan_status == 1){ // 1:点赞。2:取消点赞
			let zan = [];
			if(list.data[0].zan){
				list.data[0].zan.push(event.zan);
				zan = list.data[0].zan;
			}else{
				zan.push(event.zan)
			}
			let res = await collection.doc(event._id).update({
				zan: zan
			})
			
			
			
			return {
				code: 0,
				msg: '点赞成功',
				data: list.data
			}
		}else if(event.zan_status == 2){
			// let zan = list.data[0].zan;
			list.data[0].zan.forEach((item,index)=>{
				if(item.uid == event.zan.uid){
					list.data[0].zan.splice(index,1)
				}
			})
			let res = await collection.doc(event._id).update({
				zan: list.data[0].zan
			})
			
			
			return {
				code: 0,
				msg: '取消点赞',
				data: list.data
			}
		}
		
		
		
	}else if(event.type && event.type == 'lun'){
		let list =  await collection.doc(event._id).get();
		let comment = [];
		if(list.data[0].comment){
			list.data[0].comment.push(event.comment);
			comment = list.data[0].comment;
		}else{
			comment.push(event.comment);
			list.data[0].comment = comment
		}
		let res = await collection.doc(event._id).update({
			comment: comment
		})
		
		// 再此处理添加消息库
		let newslist =  await news.where({memor_id:event._id}).get();
		// console.log(newslist);
		// return
		if(newslist.data.length > 0){  // 证明有此条内容的消息通知
			if(newslist.data[0].news_comment){
				newslist.data[0].news_comment.unshift(event.comment); // 保存到数据头部
			}else{
				let news_comment = [];
				news_comment.unshift(event.comment);
				newslist.data[0].news_comment = news_comment; // 保存到数据头部
			}
			
			await news.where({memor_id:event._id}).update({  // 更新数据库
				is_read: 2,  // 1:已读 2:未读
				news_comment: newslist.data[0].news_comment
			})
		}else{ // 没有此条内容的消息通知
			await news.add({  // 更新数据库
				memor_id: event._id,
				memor_uid: event.memor_uid,
				memor_title: list.data[0].title,
				is_read: 2,  // 1:已读 2:未读
				news_comment: [event.news_comment]
			})
		}
		// 添加消息库end
		
		return {
			code: 0,
			msg: '评论成功',
			data: list.data
		}
	}else {
		const res = await collection.add(event)
		return res
	}

};
